using UnityEngine;
using UnityEngine.UI;

public class BlurredScreen : MonoBehaviour
{
	public Camera sourceCamera;

	[HideInInspector]
	public Image target;

	public float screenScale = 0.1f;

	public FilterMode filterMode = FilterMode.Bilinear;

	public int anisoLevel = 2;

	public bool run;

	public bool targetActive;

	public bool blurType1 = true;

	public bool blurType2 = true;

	public bool blurType3 = true;

	public bool blurType4 = true;

	public bool blurTypeSingle;

	public static BlurredScreen instance;

	private Camera captureCamera;

	public static void Show()
	{
		if (instance == null)
		{
			GameObject gameObject = GameObject.Find("BlurredScreen");
			if (gameObject != null)
			{
				instance = gameObject.GetComponent<BlurredScreen>();
			}
		}
		if ((bool)instance)
		{
			instance.Run();
		}
	}

	public static void Hide()
	{
		if ((bool)instance)
		{
			instance.targetActive = false;
			instance.target.enabled = false;
		}
	}

	private void Start()
	{
		base.gameObject.name = "BlurredScreen";
		target = base.gameObject.AddComponent<Image>();
		target.enabled = false;
		InitCaptureCamera();
	}

	private void Update()
	{
		if (run)
		{
			Run();
			run = false;
		}
		target.enabled = targetActive;
	}

	public void Run()
	{
		if (sourceCamera == null)
		{
			sourceCamera = OrbitCamera.GetInstance().camera;
			//if (sourceCamera.gameObject.GetComponent<GUILayer>() == null)
			//{
			//	sourceCamera.gameObject.AddComponent<GUILayer>();
			//}
		}
		if (sourceCamera == null)
		{
			Debug.Log("Source Camera NOT FOUND");
			return;
		}
		base.transform.position = new Vector3(0.5f, 0.5f, 0f);
		base.transform.localScale = Vector3.zero;
		float realtimeSinceStartup = Time.realtimeSinceStartup;
		target.enabled = false;
        target.sprite = Sprite.Create((Texture2D)GetBlurredSnap(), new Rect(0, 0, GetBlurredSnap().width, GetBlurredSnap().height), new Vector2(0.5f, 0.5f)); ;
        RectTransform rectTransform = target.GetComponent<RectTransform>();
        rectTransform.sizeDelta = new Vector2(Screen.width, Screen.height);
        rectTransform.anchorMin = new Vector2(0.5f, 0.5f);
        rectTransform.anchorMax = new Vector2(0.5f, 0.5f);
        rectTransform.anchoredPosition = new Vector2(0, 0);
        target.gameObject.SetActive(true);
		targetActive = true;
		Debug.Log("run took: " + (Time.realtimeSinceStartup - realtimeSinceStartup).ToString("f9"));
	}

	private void InitCaptureCamera()
	{
		if (captureCamera == null)
		{
			GameObject gameObject = new GameObject("Capture Camera", typeof(Camera));
            captureCamera = gameObject.GetComponent<Camera>();
            captureCamera.enabled = false;
			gameObject.hideFlags = HideFlags.HideAndDontSave;
		}
	}

	private Texture2D GetBlurredSnap()
	{
		captureCamera.CopyFrom(sourceCamera);
		RenderTexture renderTexture = new RenderTexture(Mathf.RoundToInt((float)Screen.width * screenScale), Mathf.RoundToInt((float)Screen.height * screenScale), 24, RenderTextureFormat.ARGB32);
		renderTexture.filterMode = filterMode;
		renderTexture.anisoLevel = anisoLevel;
		renderTexture.useMipMap = false;
		captureCamera.targetTexture = renderTexture;
		captureCamera.Render();
		RenderTexture.active = renderTexture;
		Texture2D texture2D = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.ARGB32, false);
		texture2D.filterMode = filterMode;
		texture2D.anisoLevel = anisoLevel;
		texture2D.ReadPixels(new Rect(0f, 0f, renderTexture.width, renderTexture.height), 0, 0);
		Color[] pixels = texture2D.GetPixels();
		int num = 0;
		for (int i = 0; i < renderTexture.height; i++)
		{
			for (int j = 0; j < renderTexture.width; j++)
			{
				float num2 = 0f;
				float num3 = 0f;
				float num4 = 0f;
				int num5 = 0;
				float num6 = 0f;
				float num7 = 0f;
				float num8 = 0f;
				int num9 = 0;
				if (blurType1)
				{
					int num10 = -1;
					while (num10 <= 1)
					{
						num++;
						int num11 = i + num10;
						int num12 = j + 1;
						if (num11 >= 0 && num11 < renderTexture.height && num12 >= 0 && num12 < renderTexture.width)
						{
							Color color = pixels[num11 * renderTexture.width + num12];
							num2 += color.r;
							num3 += color.g;
							num4 += color.b;
							num5++;
						}
						num11 = i + num10;
						num12 = j;
						if (num11 >= 0 && num11 < renderTexture.height && num12 >= 0 && num12 < renderTexture.width)
						{
							Color color = pixels[num11 * renderTexture.width + num12];
							num2 += color.r;
							num3 += color.g;
							num4 += color.b;
							num5++;
						}
						num11 = i + num10;
						num12 = j - 1;
						if (num11 >= 0 && num11 < renderTexture.height && num12 >= 0 && num12 < renderTexture.width)
						{
							Color color = pixels[num11 * renderTexture.width + num12];
							num2 += color.r;
							num3 += color.g;
							num4 += color.b;
							num5++;
						}
						num11 = renderTexture.height - i + num10;
						num12 = j + 1;
						if (num11 >= 0 && num11 < renderTexture.height && num12 >= 0 && num12 < renderTexture.width)
						{
							Color color = pixels[num11 * renderTexture.width + num12];
							num6 += color.r;
							num7 += color.g;
							num8 += color.b;
							num9++;
						}
						num11 = renderTexture.height - i + num10;
						num12 = j - 1;
						if (num11 >= 0 && num11 < renderTexture.height && num12 >= 0 && num12 < renderTexture.width)
						{
							Color color = pixels[num11 * renderTexture.width + num12];
							num6 += color.r;
							num7 += color.g;
							num8 += color.b;
							num9++;
						}
						num10++;
						if (num10 == 0)
						{
							num10++;
						}
					}
				}
				if (blurType2)
				{
					int num13 = 1;
					int num11 = i - num13;
					int num12 = j - num13;
					if (num11 >= 0 && num11 < renderTexture.height && num12 >= 0 && num12 < renderTexture.width)
					{
						Color color = pixels[num11 * renderTexture.width + num12];
						num2 += color.r;
						num3 += color.g;
						num4 += color.b;
						num5++;
					}
					num11 = i - num13;
					num12 = j + num13;
					if (num11 >= 0 && num11 < renderTexture.height && num12 >= 0 && num12 < renderTexture.width)
					{
						Color color = pixels[num11 * renderTexture.width + num12];
						num2 += color.r;
						num3 += color.g;
						num4 += color.b;
						num5++;
					}
					num11 = i + num13;
					num12 = j - num13;
					if (num11 >= 0 && num11 < renderTexture.height && num12 >= 0 && num12 < renderTexture.width)
					{
						Color color = pixels[num11 * renderTexture.width + num12];
						num2 += color.r;
						num3 += color.g;
						num4 += color.b;
						num5++;
					}
					num11 = i + num13;
					num12 = j + num13;
					if (num11 >= 0 && num11 < renderTexture.height && num12 >= 0 && num12 < renderTexture.width)
					{
						Color color = pixels[num11 * renderTexture.width + num12];
						num2 += color.r;
						num3 += color.g;
						num4 += color.b;
						num5++;
					}
				}
				if (blurType3)
				{
					int num14 = 2;
					int num11 = i - num14;
					int num12 = j - num14;
					if (num11 >= 0 && num11 < renderTexture.height && num12 >= 0 && num12 < renderTexture.width)
					{
						Color color = pixels[num11 * renderTexture.width + num12];
						num2 += color.r;
						num3 += color.g;
						num4 += color.b;
						num5++;
					}
					num11 = i - num14;
					num12 = j + num14;
					if (num11 >= 0 && num11 < renderTexture.height && num12 >= 0 && num12 < renderTexture.width)
					{
						Color color = pixels[num11 * renderTexture.width + num12];
						num2 += color.r;
						num3 += color.g;
						num4 += color.b;
						num5++;
					}
					num11 = i + num14;
					num12 = j - num14;
					if (num11 >= 0 && num11 < renderTexture.height && num12 >= 0 && num12 < renderTexture.width)
					{
						Color color = pixels[num11 * renderTexture.width + num12];
						num2 += color.r;
						num3 += color.g;
						num4 += color.b;
						num5++;
					}
					num11 = i + num14;
					num12 = j + num14;
					if (num11 >= 0 && num11 < renderTexture.height && num12 >= 0 && num12 < renderTexture.width)
					{
						Color color = pixels[num11 * renderTexture.width + num12];
						num2 += color.r;
						num3 += color.g;
						num4 += color.b;
						num5++;
					}
				}
				if (blurType4)
				{
					int num15 = 3;
					int num11 = i - num15;
					int num12 = j - num15;
					if (num11 >= 0 && num11 < renderTexture.height && num12 >= 0 && num12 < renderTexture.width)
					{
						Color color = pixels[num11 * renderTexture.width + num12];
						num2 += color.r;
						num3 += color.g;
						num4 += color.b;
						num5++;
					}
					num11 = i - num15;
					num12 = j + num15;
					if (num11 >= 0 && num11 < renderTexture.height && num12 >= 0 && num12 < renderTexture.width)
					{
						Color color = pixels[num11 * renderTexture.width + num12];
						num2 += color.r;
						num3 += color.g;
						num4 += color.b;
						num5++;
					}
					num11 = i + num15;
					num12 = j - num15;
					if (num11 >= 0 && num11 < renderTexture.height && num12 >= 0 && num12 < renderTexture.width)
					{
						Color color = pixels[num11 * renderTexture.width + num12];
						num2 += color.r;
						num3 += color.g;
						num4 += color.b;
						num5++;
					}
					num11 = i + num15;
					num12 = j + num15;
					if (num11 >= 0 && num11 < renderTexture.height && num12 >= 0 && num12 < renderTexture.width)
					{
						Color color = pixels[num11 * renderTexture.width + num12];
						num2 += color.r;
						num3 += color.g;
						num4 += color.b;
						num5++;
					}
				}
				if (blurType1 || blurType2 || blurType3 || blurType4)
				{
					num2 /= (float)num5;
					num3 /= (float)num5;
					num4 /= (float)num5;
					num6 /= (float)num9;
					num7 /= (float)num9;
					num8 /= (float)num9;
					pixels[i * renderTexture.width + j] = new Color(num2, num3, num4);
					if (i > 0)
					{
						pixels[(renderTexture.height - i) * renderTexture.width + j] = new Color(num6, num7, num8);
					}
				}
			}
		}
		if (!blurTypeSingle)
		{
			texture2D.SetPixels(pixels);
		}
		texture2D.Apply();
		Debug.Log("pixels = " + renderTexture.width * renderTexture.height + " loops: " + num);
		captureCamera.targetTexture = null;
		RenderTexture.active = null;
		Object.DestroyImmediate(renderTexture);
		if (blurTypeSingle)
		{
			return Blur(texture2D, 1);
		}
		return texture2D;
	}

	private Texture2D Blur(Texture2D image, int blurSize = 2)
	{
		Texture2D texture2D = new Texture2D(image.width, image.height);
		texture2D.filterMode = filterMode;
		texture2D.anisoLevel = anisoLevel;
		Color[] pixels = image.GetPixels();
		Color[] array = new Color[image.width * image.height];
		for (int i = 0; i < image.height; i++)
		{
			for (int j = 0; j < image.width; j++)
			{
				float num = 0f;
				float num2 = 0f;
				float num3 = 0f;
				int num4 = 0;
				int num5 = i - blurSize;
				int num6 = j - blurSize;
				if (num5 >= 0 && num5 < image.height && num6 >= 0 && num6 < image.width)
				{
					Color color = pixels[num5 * image.width + num6];
					num += color.r;
					num2 += color.g;
					num3 += color.b;
					num4++;
				}
				num5 = i - blurSize;
				num6 = j + blurSize;
				if (num5 >= 0 && num5 < image.height && num6 >= 0 && num6 < image.width)
				{
					Color color = pixels[num5 * image.width + num6];
					num += color.r;
					num2 += color.g;
					num3 += color.b;
					num4++;
				}
				num5 = i + blurSize;
				num6 = j - blurSize;
				if (num5 >= 0 && num5 < image.height && num6 >= 0 && num6 < image.width)
				{
					Color color = pixels[num5 * image.width + num6];
					num += color.r;
					num2 += color.g;
					num3 += color.b;
					num4++;
				}
				num5 = i + blurSize;
				num6 = j + blurSize;
				if (num5 >= 0 && num5 < image.height && num6 >= 0 && num6 < image.width)
				{
					Color color = pixels[num5 * image.width + num6];
					num += color.r;
					num2 += color.g;
					num3 += color.b;
					num4++;
				}
				num /= (float)num4;
				num2 /= (float)num4;
				num3 /= (float)num4;
				array[i * image.width + j] = new Color(num, num2, num3);
			}
		}
		texture2D.SetPixels(array);
		texture2D.Apply();
		return texture2D;
	}
}
